// pages/login/login.js
const app = getApp().globalData.userInfo
const addAddress = getApp().globalData.addressInfo
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userName: '',
        userPwd: '',
        // userState: 'false'
    },

    userName(e){
        let userData = this.data.userName
        // console.log(e)
        userData = e.detail.value
        this.setData({
            userName: userData
        })
        // console.log(this.data.userName)
    },

    pwd(e){
        let pwdData = this.data.userPwd
        // console.log(e)
        pwdData = e.detail.value
        this.setData({
            userPwd: pwdData
        })
        // console.log(this.data.userPwd)
    },

    login(e){
        var _this = this
        let userName = e.target.dataset.user_name
        let userPwd = e.target.dataset.user_password
        // let state = this.data.userState
        wx.request({
          url: 'http://localhost:8888/wxLogin',
          data: {
            'wx_username': userName,
            'wx_password': userPwd
          },
          success(res){
            console.log('请求成功',res)
            if(res.data.code == 200){
                let wx_nickname = res.data.user[0].wx_nickname
                let wx_phone = res.data.user[0].wx_phone
                let avatar = JSON.stringify(res.data.user[0].wx_avatar)
                let wx_userid = res.data.user[0].wx_userid
                let wx_username = res.data.user[0].wx_username
                let address = res.data.user[0].wx_address
                app.push(wx_nickname,wx_phone,avatar,wx_userid,wx_username,address)
                addAddress.push(wx_nickname,wx_phone,avatar,wx_userid,wx_username,address)
                wx.showToast({
                    title: '登录成功',
                    icon: 'none',
                    duration: 2000
                })
                setTimeout(function(){
                wx.switchTab({
                    url: '../person/person'
                })
                },2000)
                // console.log(_this.data.userState)
            }else{
                wx.showToast({
                  title: '登录失败，账号或密码错误',
                  icon: 'none',
                  duration: 2000
                })
            }
          },
          fail(err){
            console.log('请求失败',err)
          }
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})